//分治思想
class Solution {
public:

    TreeNode* _buildTree(vector<int>& preorder,vector<int>& inorder,int& prei,int inbegin,int inend)
    {
        if(inbegin > inend)
            return nullptr;
        int rooti = inbegin;
        while(rooti <= inend)
        {
            if(inorder[rooti] == preorder[prei])
                break;
            else
                rooti++;
        }
        TreeNode* root = new TreeNode(preorder[prei]);
        prei++;
        root->left = _buildTree(preorder,inorder,prei,inbegin,rooti-1);
        root->right = _buildTree(preorder,inorder,prei,rooti+1,inend);
        return root;
    }
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        int prei = 0;
        return _buildTree(preorder,inorder,prei,0,inorder.size()-1);
    }
};